package cn.mcch.investigator.activity;

import android.content.Intent;
import android.view.View;
import cn.mcch.investigator.R;
import cn.mcch.investigator.application.InvestigatorApplication;
import cn.mcch.investigator.dialog.LoadingDialog;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.bean.UserBean;
import cn.mcch.investigator.model.impl.LoginModelImpl;
import cn.mcch.investigator.model.impl.MainModelImpl;
import cn.mcch.investigator.presenter.impl.LoginPresenterImpl;
import cn.mcch.investigator.utils.DialogUtils;
import cn.mcch.investigator.utils.JsonUtils;
import cn.mcch.investigator.viewdelegate.impl.LoginViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/13.
 */
public class LoginActivity extends BaseActivity<LoginViewDelegateImpl, LoginModelImpl, LoginPresenterImpl> implements OnResponseListener {
   private LoadingDialog loadingDialog;

    @Override
    public Class<LoginPresenterImpl> getPresenterClass() {
        return LoginPresenterImpl.class;
    }

    @Override
    public void createFirstView() {
        mPresenter.createFirstView(getApplicationContext(), R.layout.activity_login, this);
        mPresenter.setUsername();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.sb_login:
                if (mPresenter.verify()) {
                    mPresenter.login(this);
                    loadingDialog = DialogUtils.showLoadingDialog(getSupportFragmentManager(), "正在登录，请稍后...");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onBefore(int id) {

    }

    @Override
    public void onAfter(boolean result, int id) {
        loadingDialog.dismiss();
        loadingDialog = null;
    }

    @Override
    public void onSuccess(String json, int id) {
        switch (id) {
            case 1:
                // 登录成功
                // 首先记录下手机号
                mPresenter.saveUsername();
                UserBean userBean = JsonUtils.fromJsonToObject(json,UserBean.class);
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                InvestigatorApplication.getInstance().setUserBeanbean(userBean);
                startActivity(intent);
                this.finish();
                break;
            default:
                break;
        }
    }
}
